package com.chatgpt_backend.mapper;

import com.chatgpt_backend.entity.account.Account;
import com.chatgpt_backend.entity.user.RegisterUser;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface UserMapper {

    @Select("select *from account where username = #{username} or email = #{email}")
    Account getAccountByNameOrEmail(String src);

    @Select("select id from account where email = #{email}")
    Integer isRegisterEmail(String email);

    @Select("select role from role join role_account where role.id = role_account.rid and role_account.aid = #{aid}")
    String getRoleByAid(Integer aid);

    @Insert("insert into account (username, password, email) values (#{username}, #{password}, #{email})")
    Integer insertUser(RegisterUser user);

    @Insert("insert into role_account (aid, rid) VALUES (#{aid}, 4)")
    Integer insertRole(Integer aid);

    @Select("select id from account where username = #{username}")
    Integer getAccountIdByUsername(String username);

    @Update("update account set password = #{newPassword} where email = #{email}")
    Integer resetPsdByEmail(String email, String newPassword);

}
